package com.lhkj.ct.admin.modules.psychology.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScale;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScaleSet;
import com.lhkj.ct.meta.modules.gauge.service.ScaleConfigService;
import com.lhkj.ct.meta.modules.gauge.service.ScaleSetService;
import com.lhkj.ct.meta.modules.patient.model.dto.PatientQuery;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatCrowdTag;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatUnit;
import com.lhkj.ct.meta.modules.patient.model.vo.PatientBasicVo;
import com.lhkj.ct.meta.modules.patient.service.PatientService;
import com.lhkj.ct.meta.modules.psychology.model.dto.GaugeAllotDto;
import com.lhkj.ct.meta.modules.psychology.model.dto.ScanAllotDto;
import com.lhkj.ct.meta.modules.psychology.service.PsyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *      测验分配
 * </p>
 *
 * @author job
 * @since 2023/10/12
 */
@RestController
@RequestMapping("api/plat/psychology/allot")
@Api(tags = {"2.1测验分配"})
public class PsyAllotController {

    @Resource
    private PatientService patientService;

    @Resource
    private ScaleConfigService scaleConfigService;

    @Resource
    private PsyRecordService psyRecordService;

    @Resource
    private ScaleSetService scaleSetService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "档案列表")
    @GetMapping("patients")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<IPage<PatientBasicVo>> selectPatUnitPage(PatientQuery patientQuery) {
        return ActionResult.success(patientService.getBasicUserPage(patientQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "单位树级列表")
    @GetMapping("units")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<List<TblPatUnit>> selectPatUnitTreeWithCount(TblPatUnit unit) {
        return ActionResult.success(patientService.selectPatUnitTreeWithCount(unit));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "标签列表")
    @GetMapping("tags")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<List<TblPatCrowdTag>> selectPatTagWithCount(TblPatCrowdTag tag) {
        return ActionResult.success(patientService.selectPatTagWithCount(tag));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "量表列表", notes = "按量表分类进行分组")
    @GetMapping("scales")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<List<Map<String, List<Map<String, Object>>>>> selectPatUnitPage(TblScale scale) {
        return ActionResult.success(scaleConfigService.groupByCategory(scale));
    }

    @GetMapping(value = "scaleSets")
    @ApiOperation(value = "测验组套列表")
    public ActionResult<IPage<TblScaleSet>> list(TblScaleSet scaleSet, PageQuery pageQuery) {
        return ActionResult.success(scaleSetService.selectPagesByAllot(scaleSet, pageQuery));
    }

    @ApiOperationSupport(order = 5)
    @PreventDuplicateSubmit
    @PostMapping(value = "allot")
    @ApiOperation(value = "测验分配")
    @Log(title = "测验分配", actionType = ActionType.INSERT)
//    //@RequiresPermissions("XLCP:XLCY:LBFP:MAIN")
    public ActionResult<Void> allot(@Validated({ValGroups.Insert.class, GaugeAllotDto.Scale.class}) @RequestBody GaugeAllotDto allotDto) {
        Asserts.assertFalse(CollectionUtils.isEmpty(allotDto.getPatIds())
                && CollectionUtils.isEmpty(allotDto.getPatLabelIds())
                && CollectionUtils.isEmpty(allotDto.getUnitIds()), "请选择需要分配的对象");
        return ActionResult.judge(psyRecordService.allot(allotDto));
    }

    @GetMapping(value = "scanAllot")
    @ApiOperation(value = "扫码分配")
    public void scanAllot(ScanAllotDto scanAllotDto, HttpServletResponse response) throws IOException {
        psyRecordService.scanAllot(scanAllotDto, response);
    }

}
